package net.kazed.nextaction.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;

public class TaskView extends LinearLayout {
	protected TextView mDescription;
	protected ImageView mIcon;
	protected TextView mDueDate;
	
	public TaskView(Context androidContext) {
		super(androidContext);
		
        LayoutInflater vi = (LayoutInflater)androidContext.
			getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi.inflate(R.layout.task_list_item_view, this, true); 
		
		mDescription = (TextView) findViewById(R.id.description);
		mIcon = (ImageView) findViewById(R.id.icon);
		mDueDate = (TextView) findViewById(R.id.due_date);
	}
		
	public void updateView(Task task, boolean isSelected) {
//		org.dodgybits.android.shuffle.model.Context context = task.context;
//		if (mShowContext && context != null) {
//			boolean displayIcon = Preferences.displayContextIcon(getContext());
//			// add context icon if preferences indicate to
//			Integer iconResource = context.iconResource;
//			if (iconResource != null && displayIcon) {
//				mIcon.setImageResource(iconResource);
//				mIcon.setVisibility(View.VISIBLE);
//			} else {
//				mIcon.setVisibility(View.GONE);
//			}
//			boolean displayContext = Preferences.displayContextName(getContext());
//			if (displayContext) {
//				mContext.setText(context.name);
//				mContext.setColourIndex(context.colourIndex, isSelected);
//				mContext.setVisibility(View.VISIBLE);
//			} else {
//				mContext.setVisibility(View.GONE);
//			}
//		} else {
//			mContext.setVisibility(View.GONE);
//			mIcon.setVisibility(View.GONE);
//		}		
		CharSequence description = task.getDescription();
		if (task.getComplete()) {
			// add strike-through for completed tasks
			SpannableString desc = new SpannableString(description);
			desc.setSpan(new StrikethroughSpan(), 0, description.length(), Spanned.SPAN_PARAGRAPH);
			description = desc;
			mIcon.setVisibility(View.VISIBLE);
		} else {
         mIcon.setVisibility(View.INVISIBLE);
		}
		mDescription.setText(description);
		if (task.getDueDate() != null) {
		   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			mDueDate.setText(dateFormat.format(task.getDueDate()));
			Date date = new Date();
			if (date.before(task.getDueDate())) {
				// task is overdue
				mDueDate.setTypeface(Typeface.DEFAULT_BOLD);
				mDueDate.setTextColor(Color.RED);
			} else {
				mDueDate.setTypeface(Typeface.DEFAULT);
				mDueDate.setTextColor(getContext().getResources().getColor(R.drawable.date_blue));
			}
			mDueDate.setVisibility(View.VISIBLE);
		} else {
			mDueDate.setVisibility(View.GONE);
		}
	}

	
}
